//
//  vspConnect.m
//  妙居
//
//  Created by gaogao on 19/07/2017.
//  Copyright © 2017 miotlink. All rights reserved.
//
#import "vspConnect.h"
#import "MMWMiotLink.h"


@interface vspConnect ()<GCDAsyncSocketDelegate>
{
    
    NSMutableData *_respondsData;
}

@end

@implementation vspConnect

+ (instancetype)shareInstance
{
    static vspConnect *connect = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        connect = [[vspConnect alloc] init];
    });
    return connect;
}

#pragma mark 连接IP 和 端口
- (void)getSocket:(NSString *)ip onPort:(int)port
{
    if(!_socket)
    {
        dispatch_queue_t dQueue = dispatch_queue_create("client tcp socket", NULL);
        _socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dQueue];
        //_socket.IPv4PreferredOverIPv6 = _isIPv4;
    }
    NSError *err;
    [_socket connectToHost:ip onPort:port withTimeout:10 error:&err];
}
#pragma mark - GCDAsyncSocketDelegate
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
    if ([sock connectedPort] == 9600)
    {
        [_socket readDataToLength:8 withTimeout:-1 tag:100];
        if ([self.delegate respondsToSelector:@selector(didConnectToHost)]) {
            [self.delegate didConnectToHost];
        }
        
    }
}
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
    
    
    //NSLog(@"socketDidDisconnect----%@--------------错误信息为：%@",(sock == _rsSocket)?@"rsSocket":@"cmsSocket",err);
    if (err)
    {
        if ([self.delegate respondsToSelector:@selector(failAction:)]) {
            [self.delegate failAction:err];
        }
        
    }
    
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    
    if (!_respondsData)
    {
        _respondsData = data.mutableCopy;
        char *respondsChar = (char *)[data bytes];
        
        int ThesecondData = respondsChar[2];
        
        int  Thethird = respondsChar[3];
        
        if (ThesecondData<0)
        {
            ThesecondData = ThesecondData+256;
        }
        if (Thethird<0)
        {
            Thethird = 256+Thethird;
        }
        int i = ThesecondData*256+Thethird;
        [sock readDataToLength:(i-8) withTimeout:-1 tag:150];
        
    }
    else
    {
        [self apendRemainingData:data sock:sock];
    }
}
#pragma mark - other

- (void)apendRemainingData:(NSData *)data sock:(GCDAsyncSocket *)sock
{
    [_respondsData appendData:data];
    NSData*respondsData = _respondsData;
    dispatch_queue_t dQueue = dispatch_queue_create("vsp queue", NULL);
    dispatch_async(dQueue, ^
    {
        char *pData = (char *)[respondsData bytes];
        if ([self.delegate respondsToSelector:@selector(receiveSocketData:)]) {
            [self.delegate receiveSocketData:respondsData];
        }
        
        _respondsData = nil;
        if (pData[1] == 0x1c)
        {
            
        }
        else
        {
            [sock readDataToLength:8 withTimeout:-1 buffer:[NSMutableData dataWithLength:1024] bufferOffset:4 tag:100];
        }
        
    });
}

#pragma mark 关闭tcp

- (void)closeSocket
{
    if (_socket) {
        [_socket disconnect];
        [_socket setDelegate:nil delegateQueue:NULL];
        _socket = nil;
    }
    
}

@end

